home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_10 / milam / strfdate.c < prev    next >
Text File  |  1994-07-12  |  10KB  |  221 lines

  1. /***************************************************************/
  2. /*             (c) Copyright 1993 by Stan Milam                */
  3. /*                                                             */
  4. /*                           strfdate()                        */
  5. /*                                                             */
  6. /* This is the main function in the file.  It uses format char-*/
  7. /* acters to format the date any way we want it in a supplied  */
  8. /* memory address.                                             */
  9. /*                                                             */
  10. /* Arguments:                                                  */
  11. /*   char *buffer - Memory address where we place the formatted*/
  12. /*                  date.                                      */
  13. /*   unsigned size- Max size of the buffer.  We do not exceed. */
  14. /*   char *format - The format string.  Characters preceeded   */
  15. /*                  by the % character are taken to be format  */
  16. /*                  specifiers.  The format specifiers are:    */
  17. /*                                                             */
  18. /*                    %a - Format abbreviated weekday name.    */
  19. /*                    %A - Full weekday name.                  */
  20. /*                    %b - Format abbreviated month name.      */
  21. /*                    %B - Format full month name.             */
  22. /*                    %d - Format the day of the month (01-31).*/
  23. /*                    %j - The day of the year (001 - 366).    */
  24. /*                    %m - The month of the year (01 - 12).    */
  25. /*                    %U - The week of year (00 - 52, Sunday)  */
  26. /*                    %w - The day of week (0 - 6, Sunday = 0) */
  27. /*                    %W - The week of year (00 - 52, Monday). */
  28. /*                    %x - The date (MM/DD/CCYY).              */
  29. /*                    %y - Formats a two digit year.           */
  30. /*                    %Y - Formats the four digit year.        */
  31. /*                                                             */
  32. /*   struct dt *dt - A pointer to the dt structure populated   */
  33. /*                   with the localdate() function.            */
  34. /*                                                             */
  35. /***************************************************************/
  36.  
  37. unsigned strfdate( char *buffer, unsigned size, char *format, struct dt *dt ) {
  38.  
  39.     size_t   len;
  40.     int      start;
  41.     unsigned rv = 0;
  42.     char     wrkbuf[15];
  43.     char     *src = format;
  44.     char     *dest= buffer;
  45.  
  46.     /***********************************************************/
  47.     /* Enter a loop copying characters from the format string  */
  48.     /* into the buffer unless it is a format character.        */
  49.     /***********************************************************/
  50.  
  51.     while ( *src && size ) {
  52.         if ( *src != '%' ) {
  53.             *dest++ = *src++;
  54.             size--; rv++;
  55.         }
  56.         else {
  57.             len = 0;
  58.             /***************************************************/
  59.             /* We have a format character so we handle it.     */
  60.             /***************************************************/
  61.  
  62.             switch( *++src ) {
  63.  
  64.                 /***********************************************/
  65.                 /* Just in case there is nothing following!    */
  66.                 /***********************************************/
  67.  
  68.                 case '\0' :
  69.                     continue;
  70.  
  71.                 /***********************************************/
  72.                 /* Format the abbreviated weekday name.        */
  73.                 /***********************************************/
  74.  
  75.                 case 'a' :
  76.                     len = 3;
  77.                     if ( len > size ) len = size;
  78.                     strncpy(dest, full_weekday[dt -> dt_wday], len );
  79.                     break;
  80.  
  81.                 /***********************************************/
  82.                 /* Format the full name of the week.           */
  83.                 /***********************************************/
  84.  
  85.                 case 'A' :
  86.                     len = strlen( full_weekday[dt -> dt_wday] );
  87.                     if ( len > size ) len = size;
  88.                     strncpy( dest, full_weekday[dt->dt_wday], len);
  89.                     break;
  90.  
  91.                 /***********************************************/
  92.                 /* Format the abbreviated name of the month.   */
  93.                 /***********************************************/
  94.  
  95.                 case 'b' :
  96.                     len = 3;
  97.                     if ( len > size ) len = size;
  98.                     strncpy(dest, full_month[dt -> dt_month], len);
  99.                     break;
  100.  
  101.                 /***********************************************/
  102.                 /* Format the full name of the month.          */
  103.                 /***********************************************/
  104.  
  105.                 case 'B' :
  106.                     len = strlen(full_month[dt -> dt_month]);
  107.                     if ( len > size ) len = size;
  108.                     strncpy(dest, full_month[dt -> dt_month], len);
  109.                     break;
  110.  
  111.                 /***********************************************/
  112.                 /* Format the day of the month.                */
  113.                 /***********************************************/
  114.  
  115.                 case 'd' :
  116.                     len = sprintf(wrkbuf, "%02d", dt -> dt_mday);
  117.                     if ( len > size ) len = size;
  118.                     strncpy(dest, wrkbuf, len);
  119.                     break;
  120.  
  121.                 /***********************************************/
  122.                 /* Format the day of the year (001 - 366).     */
  123.                 /***********************************************/
  124.  
  125.                 case 'j' :
  126.                     len = sprintf(wrkbuf, "%03d", dt -> dt_yday + 1);
  127.                     if ( len > size ) len = size;
  128.                     strncpy(dest, wrkbuf, len);
  129.                     break;
  130.  
  131.                 /***********************************************/
  132.                 /* Format the month of the year (01 - 12).     */
  133.                 /***********************************************/
  134.  
  135.                 case 'm' :
  136.                     len = sprintf(wrkbuf, "%02d", dt -> dt_month + 1);
  137.                     if ( len > size ) len = size;
  138.                     strncpy(dest, wrkbuf, len);
  139.                     break;
  140.  
  141.                 /***********************************************/
  142.                 /* Formats the week of the year. %U starts the */
  143.                 /* week with Sunday, %W starts the week with a */
  144.                 /* Monday.                                     */
  145.                 /***********************************************/
  146.  
  147.                 case 'U' :
  148.                 case 'W' :
  149.                     start = (*src == 'U') ? 1 : 0;
  150.                     len = sprintf(wrkbuf, "%02d",
  151.                           week_of_year(start, dt -> dt_wday, dt->dt_yday));
  152.                     if ( len > size ) len = size;
  153.                     strncpy(dest, wrkbuf, len);
  154.                     break;
  155.  
  156.                 /***********************************************/
  157.                 /* Format the day of the week (1 - 6).         */
  158.                 /***********************************************/
  159.  
  160.                 case 'w' :
  161.                     len = sprintf(wrkbuf, "%d", dt -> dt_wday);
  162.                     if ( len > size ) len = size;
  163.                     strncpy( dest, wrkbuf, len );
  164.                     break;
  165.  
  166.                 /***********************************************/
  167.                 /* Formats a traditional Gregorian date with   */
  168.                 /* single exception: The year is four digits.  */
  169.                 /***********************************************/
  170.  
  171.                 case 'x' :
  172.                     len = sprintf(wrkbuf, "%02d/%02d/%04d",
  173.                                   dt -> dt_month + 1,
  174.                                   dt -> dt_mday,
  175.                                   dt -> dt_year);
  176.                     if (len > size) len = size;
  177.                     strncpy(dest, wrkbuf, len);
  178.                     break;
  179.  
  180.                 /***********************************************/
  181.